function getRect(selector) {
    return new Promise((resolve) => {
        wx.createSelectorQuery()
            .in(this)
            .select(selector)
            .boundingClientRect()
            .exec((rect = []) => resolve(rect[0]));
    });
}
function getAllRect(selector) {
    return new Promise((resolve) => {
        wx.createSelectorQuery()
            .in(this)
            .selectAll(selector)
            .boundingClientRect()
            .exec((rect = []) => resolve(rect[0]));
    });
}
module.exports = {
    //选取元素
    getRect
    // 选取所有元素
    ,getAllRect
    // 滚动到居中位置
    ,scrollIntoView({
        //当前索引
        scrollIndex
        // scroll-view class或者 id
        ,scrollClass
        // scroll-view item class
        ,scrollItemClass
        // scroll-view data  scrollleft -- model
        ,scrollLeftModel
    }) {
        return Promise.all([
            getAllRect.call(this, scrollItemClass),
            getRect.call(this, scrollClass),
        ]).then(([tabRects, navRect]) => {
            const tabRect = tabRects[scrollIndex];
            const offsetLeft = tabRects
                .slice(0, scrollIndex)
                .reduce((prev, curr) => prev + curr.width, 0);
                const scrollToLeft = offsetLeft - (navRect.width - tabRect.width) / 2;
            if( scrollLeftModel ){
                this.$model( undefined,{
                    [scrollLeftModel]: scrollToLeft
                });
            }
            return Promise.resolve( scrollLeftModel );
            
        });
    },
}